<?php
declare(strict_types=1);

use think\migration\Migrator;
use think\migration\db\Column;

class CreateAllTables extends Migrator
{
    public function change()
    {
        // 生活服务配置表
        if (!$this->hasTable('life_service_config')) {
            $this->table('life_service_config', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('business_type', 'integer', ['comment' => '业务类型'])
                ->addColumn('service_name', 'string', ['limit' => 50, 'comment' => '服务名称'])
                ->addColumn('service_desc', 'string', ['limit' => 255, 'comment' => '服务描述'])
                ->addColumn('sort', 'integer', ['default' => 0, 'comment' => '排序'])
                ->addColumn('status', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '状态 1正常 2禁用'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'business_type'])
                ->create();
        }

        // 用户表
        if (!$this->hasTable('user')) {
            $this->table('user', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('real_name', 'string', ['limit' => 50, 'null' => true, 'comment' => '真实姓名'])
                ->addColumn('id_card', 'string', ['limit' => 18, 'null' => true, 'comment' => '身份证号'])
                ->addColumn('unionid', 'string', ['limit' => 64, 'null' => true, 'comment' => '微信unionid'])
                ->addColumn('openid', 'string', ['limit' => 64, 'null' => true, 'comment' => '微信openid'])
                ->addColumn('mp_openid', 'string', ['limit' => 64, 'null' => true, 'comment' => '公众号openid'])
                ->addColumn('balance', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => '0.00', 'comment' => '余额'])
                ->addColumn('mobile', 'string', ['limit' => 11, 'null' => true, 'comment' => '手机号'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id'])
                ->addIndex(['mobile'])
                ->addIndex(['openid'])
                ->create();
        }

        // 用户地址表
        if (!$this->hasTable('user_address')) {
            $this->table('user_address', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('address_type', 'integer', ['limit' => 1, 'comment' => '地址类型 1寄件地址 2收件地址'])
                ->addColumn('province', 'string', ['limit' => 50, 'comment' => '省份'])
                ->addColumn('city', 'string', ['limit' => 50, 'comment' => '城市'])
                ->addColumn('county', 'string', ['limit' => 50, 'comment' => '区县'])
                ->addColumn('address', 'string', ['limit' => 255, 'comment' => '详细地址'])
                ->addColumn('username', 'string', ['limit' => 50, 'comment' => '收件人姓名'])
                ->addColumn('phone', 'string', ['limit' => 11, 'comment' => '收件人电话'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'user_id'])
                ->create();
        }

        // 订单表
        if (!$this->hasTable('order')) {
            $this->table('order', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('company_code', 'string', ['limit' => 50, 'comment' => '快递公司代码'])
                ->addColumn('business_type', 'string', ['limit' => 50, 'comment' => '业务类型'])
                ->addColumn('sender_province', 'string', ['limit' => 50, 'comment' => '寄件人省份'])
                ->addColumn('sender_city', 'string', ['limit' => 50, 'comment' => '寄件人城市'])
                ->addColumn('sender_county', 'string', ['limit' => 50, 'comment' => '寄件人区县'])
                ->addColumn('sender_address', 'string', ['limit' => 255, 'comment' => '寄件人详细地址'])
                ->addColumn('sender_name', 'string', ['limit' => 50, 'comment' => '寄件人姓名'])
                ->addColumn('sender_phone', 'string', ['limit' => 11, 'comment' => '寄件人电话'])
                ->addColumn('receiver_province', 'string', ['limit' => 50, 'comment' => '收件人省份'])
                ->addColumn('receiver_city', 'string', ['limit' => 50, 'comment' => '收件人城市'])
                ->addColumn('receiver_county', 'string', ['limit' => 50, 'comment' => '收件人区县'])
                ->addColumn('receiver_address', 'string', ['limit' => 255, 'comment' => '收件人详细地址'])
                ->addColumn('receiver_name', 'string', ['limit' => 50, 'comment' => '收件人姓名'])
                ->addColumn('receiver_phone', 'string', ['limit' => 11, 'comment' => '收件人电话'])
                ->addColumn('order_status', 'integer', ['comment' => '订单状态'])
                ->addColumn('coupon_id', 'integer', ['null' => true, 'comment' => '优惠券ID'])
                ->addColumn('coupon_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => '0.00', 'comment' => '优惠金额'])
                ->addColumn('weight_estimate', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '预估重量'])
                ->addColumn('fee_estimate', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '预估运费'])
                ->addColumn('weight_actual', 'decimal', ['precision' => 10, 'scale' => 2, 'null' => true, 'comment' => '实际重量'])
                ->addColumn('fee_actual', 'decimal', ['precision' => 10, 'scale' => 2, 'null' => true, 'comment' => '实际运费'])
                ->addColumn('insure_fee', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => '0.00', 'comment' => '保价费'])
                ->addColumn('merchant_profit', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => '0.00', 'comment' => '商家利润'])
                ->addColumn('platform_profit', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => '0.00', 'comment' => '平台利润'])
                ->addColumn('remark', 'text', ['null' => true, 'comment' => '备注'])
                ->addColumn('insure_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => '0.00', 'comment' => '保价金额'])
                ->addColumn('courier_name', 'string', ['limit' => 50, 'null' => true, 'comment' => '快递员姓名'])
                ->addColumn('courier_phone', 'string', ['limit' => 11, 'null' => true, 'comment' => '快递员电话'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id'])
                ->addIndex(['user_id'])
                ->addIndex(['order_status'])
                ->addIndex(['create_time'])
                ->create();
        }

        // 优惠券表
        if (!$this->hasTable('coupon')) {
            $this->table('coupon', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('coupon_name', 'string', ['limit' => 100, 'comment' => '优惠券名称'])
                ->addColumn('coupon_type', 'integer', ['comment' => '优惠券类型'])
                ->addColumn('coupon_value', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '优惠券面值'])
                ->addColumn('valid_start_time', 'datetime', ['comment' => '有效期开始时间'])
                ->addColumn('valid_end_time', 'datetime', ['comment' => '有效期结束时间'])
                ->addColumn('usage_rules', 'text', ['comment' => '使用规则'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id'])
                ->addIndex(['valid_end_time'])
                ->create();
        }

        // 用户优惠券表
        if (!$this->hasTable('user_coupon')) {
            $this->table('user_coupon', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('coupon_id', 'integer', ['comment' => '优惠券ID'])
                ->addColumn('coupon_name', 'string', ['limit' => 100, 'comment' => '优惠券名称'])
                ->addColumn('coupon_value', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '优惠券面值'])
                ->addColumn('valid_start_time', 'datetime', ['comment' => '有效期开始时间'])
                ->addColumn('valid_end_time', 'datetime', ['comment' => '有效期结束时间'])
                ->addColumn('usage_rules', 'text', ['comment' => '使用规则'])
                ->addColumn('status', 'integer', ['comment' => '状态 1未使用 2已使用 3已过期'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'user_id'])
                ->addIndex(['coupon_id'])
                ->addIndex(['status'])
                ->create();
        }

        // 用户余额流水表
        if (!$this->hasTable('user_balance_flow')) {
            $this->table('user_balance_flow', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('order_no', 'string', ['limit' => 50, 'comment' => '订单号'])
                ->addColumn('type', 'integer', ['comment' => '类型 1充值 2提现 3订单支付 4订单退款 5分佣'])
                ->addColumn('balance_change', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '变动金额'])
                ->addColumn('balance_before', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '变动前余额'])
                ->addColumn('balance_after', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '变动后余额'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addIndex(['merchant_id', 'user_id'])
                ->addIndex(['order_no'])
                ->addIndex(['type'])
                ->create();
        }

        // 用户付款记录表
        if (!$this->hasTable('user_payment_record')) {
            $this->table('user_payment_record', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('order_no', 'string', ['limit' => 50, 'comment' => '订单号'])
                ->addColumn('business_type', 'integer', ['comment' => '业务类型 1快递费 2余额充值'])
                ->addColumn('payment_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '支付金额'])
                ->addColumn('payment_time', 'datetime', ['comment' => '支付时间'])
                ->addColumn('payment_method', 'string', ['limit' => 20, 'comment' => '支付方式'])
                ->addColumn('payment_status', 'integer', ['comment' => '支付状态'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'user_id'])
                ->addIndex(['order_no'])
                ->addIndex(['payment_status'])
                ->create();
        }

        // 用户退款记录表
        if (!$this->hasTable('user_refund_record')) {
            $this->table('user_refund_record', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('order_no', 'string', ['limit' => 50, 'comment' => '订单号'])
                ->addColumn('business_type', 'integer', ['comment' => '业务类型 1快递费 2余额充值'])
                ->addColumn('refund_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '退款金额'])
                ->addColumn('refund_time', 'datetime', ['comment' => '退款时间'])
                ->addColumn('refund_method', 'string', ['limit' => 20, 'comment' => '退款方式'])
                ->addColumn('refund_status', 'integer', ['comment' => '退款状态'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'user_id'])
                ->addIndex(['order_no'])
                ->addIndex(['refund_status'])
                ->create();
        }

        // 用户提现记录表
        if (!$this->hasTable('user_withdraw_record')) {
            $this->table('user_withdraw_record', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('withdraw_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '提现金额'])
                ->addColumn('withdraw_fee', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '手续费'])
                ->addColumn('withdraw_status', 'integer', ['comment' => '提现状态'])
                ->addColumn('withdraw_name', 'string', ['limit' => 50, 'comment' => '收款人姓名'])
                ->addColumn('withdraw_account', 'string', ['limit' => 50, 'comment' => '收款账号'])
                ->addColumn('withdraw_account_type', 'string', ['limit' => 20, 'comment' => '账号类型'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'user_id'])
                ->addIndex(['withdraw_status'])
                ->create();
        }

        // 商家表
        if (!$this->hasTable('merchant')) {
            $this->table('merchant', ['engine' => 'InnoDB'])
                ->addColumn('balance', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => '0.00', 'comment' => '余额'])
                ->addColumn('merchant_name', 'string', ['limit' => 100, 'comment' => '商家名称'])
                ->addColumn('merchant_type', 'integer', ['comment' => '商家类型 1个人 2企业'])
                ->addColumn('contact_name', 'string', ['limit' => 50, 'comment' => '联系人姓名'])
                ->addColumn('contact_phone', 'string', ['limit' => 11, 'comment' => '联系电话'])
                ->addColumn('email', 'string', ['limit' => 100, 'null' => true, 'comment' => '邮箱'])
                ->addColumn('status', 'integer', ['comment' => '状态 1正常 2禁用'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_name'])
                ->addIndex(['status'])
                ->create();
        }

        // 会员套餐表
        if (!$this->hasTable('member_package')) {
            $this->table('member_package', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('package_name', 'string', ['limit' => 50, 'comment' => '套餐名称'])
                ->addColumn('package_price', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '套餐价格'])
                ->addColumn('package_desc', 'text', ['comment' => '套餐描述'])
                ->addColumn('discount_rate', 'decimal', ['precision' => 5, 'scale' => 2, 'comment' => '快递折扣率'])
                ->addColumn('valid_time', 'integer', ['comment' => '有效期(天)'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id'])
                ->create();
        }

        // 商家应用表
        if (!$this->hasTable('merchant_application')) {
            $this->table('merchant_application', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('application_name', 'string', ['limit' => 50, 'comment' => '应用名称'])
                ->addColumn('application_type', 'integer', ['comment' => '应用类型 1微信小程序 2公众号 3pc站 4企业微信通讯录 5企业微信应用'])
                ->addColumn('app_id', 'string', ['limit' => 100, 'comment' => '应用ID'])
                ->addColumn('app_secret', 'string', ['limit' => 100, 'comment' => '应用密钥'])
                ->addColumn('token', 'string', ['limit' => 100, 'null' => true, 'comment' => '令牌'])
                ->addColumn('encoding_aes_key', 'string', ['limit' => 100, 'null' => true, 'comment' => '加密密钥'])
                ->addColumn('additional_info', 'text', ['null' => true, 'comment' => '附加信息'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'application_type'])
                ->create();
        }

        // 商家支付配置表
        if (!$this->hasTable('merchant_payment_config')) {
            $this->table('merchant_payment_config', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('payment_method', 'integer', ['comment' => '支付方式 1微信支付 2支付宝支付 3余额支付'])
                ->addColumn('payment_config', 'json', ['comment' => '支付配置'])
                ->addColumn('status', 'integer', ['comment' => '状态 1正常 2禁用'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'payment_method'])
                ->addIndex(['status'])
                ->create();
        }

        // 快递公司表
        if (!$this->hasTable('express_company')) {
            $this->table('express_company', ['engine' => 'InnoDB'])
                ->addColumn('logo', 'string', ['limit' => 255, 'comment' => 'logo'])
                ->addColumn('company_name', 'string', ['limit' => 50, 'comment' => '快递公司名称'])
                ->addColumn('company_code', 'string', ['limit' => 20, 'comment' => '快递公司编码'])
                ->addColumn('company_type', 'integer', ['comment' => '快递公司类型 1快递 2物流 3同城配送'])
                ->addColumn('description', 'text', ['comment' => '服务说明'])
                ->addColumn('is_insured', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '是否支持保价'])
                ->addColumn('insure_fee', 'decimal', ['precision' => 5, 'scale' => 2, 'default' => '0.00', 'comment' => '保价费率'])
                ->addColumn('status', 'integer', ['comment' => '状态 1正常 2禁用'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['company_code'])
                ->addIndex(['status'])
                ->create();
        }

        // 快递服务商表
        if (!$this->hasTable('express_service_provider')) {
            $this->table('express_service_provider', ['engine' => 'InnoDB'])
                ->addColumn('service_code', 'string', ['limit' => 20, 'comment' => '服务商代码'])
                ->addColumn('company_name', 'string', ['limit' => 50, 'comment' => '快递公司名称'])
                ->addColumn('company_code', 'string', ['limit' => 20, 'comment' => '快递公司编码'])
                ->addColumn('service_type', 'string', ['limit' => 50, 'comment' => '服务类型'])
                ->addColumn('channel', 'string', ['limit' => 50, 'comment' => '渠道信息'])
                ->addColumn('description', 'text', ['comment' => '服务说明'])
                ->addColumn('weight_index', 'integer', ['default' => 6000, 'comment' => '抛重系数'])
                ->addColumn('is_insured', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '是否支持保价'])
                ->addColumn('insure_fee', 'decimal', ['precision' => 5, 'scale' => 2, 'default' => '0.00', 'comment' => '保价费率'])
                ->addColumn('status', 'integer', ['comment' => '状态 1正常 2禁用'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['service_code'])
                ->addIndex(['company_code'])
                ->addIndex(['status'])
                ->create();
        }

        // 快递价格缓存表
        if (!$this->hasTable('express_price_cache')) {
            $this->table('express_price_cache', ['engine' => 'InnoDB'])
                ->addColumn('express_service_provider_id', 'integer', ['comment' => '快递服务商ID'])
                ->addColumn('from_province', 'string', ['limit' => 50, 'comment' => '寄件省份'])
                ->addColumn('from_city', 'string', ['limit' => 50, 'comment' => '寄件城市'])
                ->addColumn('to_province', 'string', ['limit' => 50, 'comment' => '收件省份'])
                ->addColumn('to_city', 'string', ['limit' => 50, 'comment' => '收件城市'])
                ->addColumn('first_price', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '首重价格'])
                ->addColumn('continued_price', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '续重价格'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['express_service_provider_id'])
                ->addIndex(['from_province', 'from_city', 'to_province', 'to_city'])
                ->create();
        }

        // 平台操作日志表
        if (!$this->hasTable('platform_operation_log')) {
            $this->table('platform_operation_log', ['engine' => 'InnoDB'])
                ->addColumn('admin_id', 'integer', ['comment' => '管理员ID'])
                ->addColumn('operation_type', 'integer', ['comment' => '操作类型 1新增 2修改 3删除'])
                ->addColumn('operation_content', 'text', ['comment' => '操作内容'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addIndex(['admin_id'])
                ->addIndex(['operation_type'])
                ->create();
        }

        // 平台配置表
        if (!$this->hasTable('platform_config')) {
            $this->table('platform_config', ['engine' => 'InnoDB'])
                ->addColumn('group_name', 'string', ['limit' => 50, 'comment' => '配置组名称'])
                ->addColumn('config_key', 'string', ['limit' => 50, 'comment' => '配置键'])
                ->addColumn('config_value', 'text', ['comment' => '配置值'])
                ->addColumn('description', 'string', ['limit' => 255, 'comment' => '描述'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['group_name', 'config_key'], ['unique' => true])
                ->create();
        }

        // 领券分润订单表
        if (!$this->hasTable('coupon_order')) {
            $this->table('coupon_order', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('service_id', 'integer', ['comment' => '分佣服务商代码 1聚推客 2快递'])
                ->addColumn('business_type', 'integer', ['comment' => '业务类型 1快递 2滴滴打车 3花小猪 4美团 5饿了么 6淘宝 7京东 8拼多多 9抖音'])
                ->addColumn('title', 'string', ['limit' => 255, 'comment' => '分佣标题'])
                ->addColumn('order_no', 'string', ['limit' => 50, 'comment' => '订单号'])
                ->addColumn('order_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '订单金额'])
                ->addColumn('merchant_commission', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '商家佣金'])
                ->addColumn('platform_commission', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '平台佣金'])
                ->addColumn('status', 'integer', ['comment' => '状态 0未支付 1已支付 2已取消'])
                ->addColumn('settlement_time', 'datetime', ['null' => true, 'comment' => '佣金结算时间'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'user_id'])
                ->addIndex(['order_no'])
                ->addIndex(['status'])
                ->create();
        }

        // 消息设置表
        if (!$this->hasTable('message_setting')) {
            $this->table('message_setting', ['engine' => 'InnoDB'])
                ->addColumn('type', 'integer', ['comment' => '通知类型'])
                ->addColumn('scene', 'integer', ['comment' => '通知场景'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['type', 'scene'], ['unique' => true])
                ->create();
        }

        // 平台消息模板配置表
        if (!$this->hasTable('message_template_config')) {
            $this->table('message_template_config', ['engine' => 'InnoDB'])
                ->addColumn('message_setting_id', 'integer', ['comment' => '消息设置ID'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('message_type', 'integer', ['comment' => '消息类型 1公众号消息 2微信小程序 3短信'])
                ->addColumn('template_no', 'string', ['limit' => 50, 'comment' => '模板编号'])
                ->addColumn('template_id', 'string', ['limit' => 100, 'comment' => '模板消息ID'])
                ->addColumn('template_name', 'string', ['limit' => 100, 'comment' => '模板消息名称'])
                ->addColumn('template_content', 'text', ['comment' => '模板消息内容'])
                ->addColumn('status', 'integer', ['comment' => '状态 1正常 2禁用'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'message_type'])
                ->addIndex(['template_id'])
                ->addIndex(['status'])
                ->create();
        }

        // 快递100订单表
        if (!$this->hasTable('kuaidi100_order')) {
            $this->table('kuaidi100_order', ['engine' => 'InnoDB'])
                ->addColumn('task_id', 'string', ['limit' => 50, 'comment' => '任务ID'])
                ->addColumn('order_id', 'string', ['limit' => 50, 'comment' => '订单ID'])
                ->addColumn('kuaidi_com', 'string', ['limit' => 20, 'comment' => '快递公司'])
                ->addColumn('kuaidi_num', 'string', ['limit' => 50, 'comment' => '快递单号'])
                ->addColumn('send_name', 'string', ['limit' => 50, 'comment' => '寄件人姓名'])
                ->addColumn('send_mobile', 'string', ['limit' => 11, 'comment' => '寄件人联系方式'])
                ->addColumn('send_province', 'string', ['limit' => 50, 'comment' => '寄件人省份'])
                ->addColumn('send_city', 'string', ['limit' => 50, 'comment' => '寄件人城市'])
                ->addColumn('send_district', 'string', ['limit' => 50, 'comment' => '寄件人区'])
                ->addColumn('send_addr', 'string', ['limit' => 255, 'comment' => '寄件人详细地址'])
                ->addColumn('rec_name', 'string', ['limit' => 50, 'comment' => '收件人姓名'])
                ->addColumn('rec_mobile', 'string', ['limit' => 11, 'comment' => '收件人联系方式'])
                ->addColumn('rec_province', 'string', ['limit' => 50, 'comment' => '收件人省份'])
                ->addColumn('rec_city', 'string', ['limit' => 50, 'comment' => '收件人城市'])
                ->addColumn('rec_district', 'string', ['limit' => 50, 'comment' => '收件人区'])
                ->addColumn('rec_addr', 'string', ['limit' => 255, 'comment' => '收件人详细地址'])
                ->addColumn('valins', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '保价费用'])
                ->addColumn('cargo', 'string', ['limit' => 255, 'comment' => '物品名称'])
                ->addColumn('service_type', 'string', ['limit' => 50, 'comment' => '业务类型'])
                ->addColumn('pre_weight', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '预估重量'])
                ->addColumn('last_weight', 'decimal', ['precision' => 10, 'scale' => 2, 'null' => true, 'comment' => '最终重量'])
                ->addColumn('comment', 'text', ['null' => true, 'comment' => '备注内容'])
                ->addColumn('day_type', 'string', ['limit' => 20, 'comment' => '预约日期'])
                ->addColumn('pickup_start_time', 'string', ['limit' => 20, 'comment' => '预约时间'])
                ->addColumn('pickup_end_time', 'string', ['limit' => 20, 'comment' => '预约截止时间'])
                ->addColumn('payment', 'string', ['limit' => 20, 'comment' => '支付方式'])
                ->addColumn('freight', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '运费'])
                ->addColumn('courier_name', 'string', ['limit' => 50, 'null' => true, 'comment' => '快递员姓名'])
                ->addColumn('courier_mobile', 'string', ['limit' => 11, 'null' => true, 'comment' => '快递员联系方式'])
                ->addColumn('status', 'integer', ['comment' => '订单状态'])
                ->addColumn('pay_status', 'integer', ['comment' => '支付状态'])
                ->addColumn('def_price', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '标准运费'])
                ->addColumn('fee_details', 'json', ['comment' => '费用明细'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['task_id'])
                ->addIndex(['order_id'])
                ->addIndex(['kuaidi_num'])
                ->addIndex(['status'])
                ->addIndex(['pay_status'])
                ->create();
        }

        // 省市县表
        if (!$this->hasTable('area')) {
            $this->table('area', ['engine' => 'InnoDB'])
                ->addColumn('area_name', 'string', ['limit' => 50, 'comment' => '地区名称'])
                ->addColumn('area_code', 'string', ['limit' => 6, 'comment' => '地区编码'])
                ->addColumn('parent_id', 'integer', ['default' => 0, 'comment' => '父级ID'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['area_code'], ['unique' => true])
                ->addIndex(['parent_id'])
                ->create();
        }

        // 管理员表
        if (!$this->hasTable('admin')) {
            $this->table('admin', ['engine' => 'InnoDB'])
                ->addColumn('username', 'string', ['limit' => 50, 'comment' => '用户名'])
                ->addColumn('password', 'string', ['limit' => 255, 'comment' => '密码'])
                ->addColumn('role_id', 'integer', ['comment' => '角色ID'])
                ->addColumn('status', 'integer', ['comment' => '状态 1正常 2禁用'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['username'], ['unique' => true])
                ->addIndex(['role_id'])
                ->addIndex(['status'])
                ->create();
        }

        // 角色表
        if (!$this->hasTable('role')) {
            $this->table('role', ['engine' => 'InnoDB'])
                ->addColumn('role_name', 'string', ['limit' => 50, 'comment' => '角色名称'])
                ->addColumn('role_desc', 'string', ['limit' => 255, 'comment' => '角色描述'])
                ->addColumn('status', 'integer', ['comment' => '状态 1正常 2禁用'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['role_name'], ['unique' => true])
                ->addIndex(['status'])
                ->create();
        }

        // 菜单配置表
        if (!$this->hasTable('menu_config')) {
            $this->table('menu_config', ['engine' => 'InnoDB'])
                ->addColumn('menu_name', 'string', ['limit' => 50, 'comment' => '菜单名称'])
                ->addColumn('menu_type', 'integer', ['comment' => '菜单类型 1按钮 2链接'])
                ->addColumn('menu_url', 'string', ['limit' => 255, 'comment' => '菜单链接'])
                ->addColumn('menu_icon', 'string', ['limit' => 50, 'comment' => '菜单图标'])
                ->addColumn('menu_order', 'integer', ['default' => 0, 'comment' => '菜单排序'])
                ->addColumn('status', 'integer', ['comment' => '状态 1正常 2禁用'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['menu_type'])
                ->addIndex(['menu_order'])
                ->addIndex(['status'])
                ->create();
        }

        // 商家充值记录表
        if (!$this->hasTable('merchant_recharge_record')) {
            $this->table('merchant_recharge_record', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('recharge_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '充值金额'])
                ->addColumn('recharge_time', 'datetime', ['comment' => '充值时间'])
                ->addColumn('status', 'integer', ['comment' => '状态 0未支付 1已支付 2已充值 3已取消'])
                ->addColumn('recharge_type', 'integer', ['comment' => '充值方式 1微信 2支付宝 3对公转账'])
                ->addColumn('transfer_no', 'string', ['limit' => 100, 'null' => true, 'comment' => '转账流水号'])
                ->addColumn('admin_id', 'integer', ['null' => true, 'comment' => '审核人admin_id'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id'])
                ->addIndex(['status'])
                ->addIndex(['recharge_type'])
                ->create();
        }

        // 商家资金流水表
        if (!$this->hasTable('merchant_fund_flow')) {
            $this->table('merchant_fund_flow', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('fund_flow_type', 'integer', ['comment' => '资金流水类型 1充值 2提现 3订单支付 4订单退款 5分佣'])
                ->addColumn('order_no', 'string', ['limit' => 50, 'comment' => '订单号'])
                ->addColumn('fund_flow_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '资金流水金额'])
                ->addColumn('fund_flow_time', 'datetime', ['comment' => '资金流水时间'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id'])
                ->addIndex(['fund_flow_type'])
                ->addIndex(['order_no'])
                ->create();
        }

        // 充值金额配置表
        if (!$this->hasTable('recharge_amount')) {
            $this->table('recharge_amount', ['engine' => 'InnoDB'])
                ->addColumn('recharge_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '充值金额'])
                ->addColumn('sort', 'integer', ['default' => 0, 'comment' => '排序'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['sort'])
                ->create();
        }

        // 用户套餐表
        if (!$this->hasTable('user_package')) {
            $this->table('user_package', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('package_id', 'integer', ['comment' => '套餐ID'])
                ->addColumn('package_name', 'string', ['limit' => 50, 'comment' => '套餐名称'])
                ->addColumn('package_price', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '套餐价格'])
                ->addColumn('package_desc', 'text', ['comment' => '套餐描述'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'user_id'])
                ->addIndex(['package_id'])
                ->create();
        }

        // 角色菜单关联表
        if (!$this->hasTable('role_menu')) {
            $this->table('role_menu', ['engine' => 'InnoDB'])
                ->addColumn('role_id', 'integer', ['comment' => '角色ID'])
                ->addColumn('menu_id', 'integer', ['comment' => '菜单ID'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addIndex(['role_id', 'menu_id'], ['unique' => true])
                ->create();
        }

        // 系统通知表
        if (!$this->hasTable('system_notification')) {
            $this->table('system_notification', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('title', 'string', ['limit' => 100, 'comment' => '通知标题'])
                ->addColumn('content', 'text', ['comment' => '通知内容'])
                ->addColumn('type', 'integer', ['comment' => '通知类型 1系统通知 2订单通知 3活动通知'])
                ->addColumn('is_read', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '是否已读'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'user_id'])
                ->addIndex(['type'])
                ->addIndex(['is_read'])
                ->create();
        }

        // 商家提现记录表
        if (!$this->hasTable('merchant_withdraw_record')) {
            $this->table('merchant_withdraw_record', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('withdraw_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '提现金额'])
                ->addColumn('withdraw_fee', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '手续费'])
                ->addColumn('withdraw_status', 'integer', ['comment' => '提现状态 0待审核 1审核通过 2审核拒绝'])
                ->addColumn('withdraw_name', 'string', ['limit' => 50, 'comment' => '收款人姓名'])
                ->addColumn('withdraw_account', 'string', ['limit' => 50, 'comment' => '收款账号'])
                ->addColumn('withdraw_account_type', 'string', ['limit' => 20, 'comment' => '账号类型'])
                ->addColumn('admin_id', 'integer', ['null' => true, 'comment' => '审核人admin_id'])
                ->addColumn('remark', 'string', ['limit' => 255, 'null' => true, 'comment' => '备注'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id'])
                ->addIndex(['withdraw_status'])
                ->create();
        }

        // 商家结算配置表
        if (!$this->hasTable('merchant_settlement_config')) {
            $this->table('merchant_settlement_config', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('min_withdraw', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '最低提现金额'])
                ->addColumn('max_withdraw', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '最高提现金额'])
                ->addColumn('withdraw_fee_rate', 'decimal', ['precision' => 5, 'scale' => 2, 'comment' => '提现手续费率'])
                ->addColumn('settlement_cycle', 'integer', ['comment' => '结算周期(天)'])
                ->addColumn('status', 'integer', ['comment' => '状态 1正常 2禁用'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id'], ['unique' => true])
                ->addIndex(['status'])
                ->create();
        }

        // 商家结算记录表
        if (!$this->hasTable('merchant_settlement_record')) {
            $this->table('merchant_settlement_record', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('settlement_no', 'string', ['limit' => 50, 'comment' => '结算单号'])
                ->addColumn('settlement_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '结算金额'])
                ->addColumn('settlement_start_time', 'datetime', ['comment' => '结算开始时间'])
                ->addColumn('settlement_end_time', 'datetime', ['comment' => '结算结束时间'])
                ->addColumn('settlement_status', 'integer', ['comment' => '结算状态 0待结算 1已结算 2结算失败'])
                ->addColumn('remark', 'string', ['limit' => 255, 'null' => true, 'comment' => '备注'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id'])
                ->addIndex(['settlement_no'])
                ->addIndex(['settlement_status'])
                ->create();
        }

        // 用户积分表
        if (!$this->hasTable('user_points')) {
            $this->table('user_points', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('points', 'integer', ['default' => 0, 'comment' => '积分'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'user_id'], ['unique' => true])
                ->create();
        }

        // 用户积分流水表
        if (!$this->hasTable('user_points_flow')) {
            $this->table('user_points_flow', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('user_id', 'integer', ['comment' => '用户ID'])
                ->addColumn('points_change', 'integer', ['comment' => '积分变动'])
                ->addColumn('points_before', 'integer', ['comment' => '变动前积分'])
                ->addColumn('points_after', 'integer', ['comment' => '变动后积分'])
                ->addColumn('change_type', 'integer', ['comment' => '变动类型 1下单 2评价 3活动'])
                ->addColumn('order_no', 'string', ['limit' => 50, 'null' => true, 'comment' => '关联订单号'])
                ->addColumn('remark', 'string', ['limit' => 255, 'comment' => '备注'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addIndex(['merchant_id', 'user_id'])
                ->addIndex(['change_type'])
                ->addIndex(['order_no'])
                ->create();
        }

        // 积分规则配置表
        if (!$this->hasTable('points_rule_config')) {
            $this->table('points_rule_config', ['engine' => 'InnoDB'])
                ->addColumn('merchant_id', 'integer', ['comment' => '商家ID'])
                ->addColumn('rule_name', 'string', ['limit' => 50, 'comment' => '规则名称'])
                ->addColumn('rule_type', 'integer', ['comment' => '规则类型 1下单 2评价 3活动'])
                ->addColumn('points', 'integer', ['comment' => '积分值'])
                ->addColumn('status', 'integer', ['comment' => '状态 1启用 2禁用'])
                ->addColumn('create_time', 'datetime', ['comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['comment' => '更新时间'])
                ->addIndex(['merchant_id', 'rule_type'])
                ->addIndex(['status'])
                ->create();
        }

        // 创建管理员登录日志表
        $this->table('admin_login_log', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci'])
            ->addColumn('id', 'integer', ['signed' => false, 'identity' => true, 'comment' => '主键ID'])
            ->addColumn('admin_id', 'integer', ['signed' => false, 'null' => false, 'default' => 0, 'comment' => '管理员ID'])
            ->addColumn('login_ip', 'string', ['limit' => 15, 'null' => false, 'default' => '', 'comment' => '登录IP'])
            ->addColumn('login_time', 'datetime', ['null' => false, 'comment' => '登录时间'])
            ->addColumn('create_time', 'datetime', ['null' => false, 'comment' => '创建时间'])
            ->addIndex(['admin_id'], ['name' => 'idx_admin_id'])
            ->addIndex(['login_time'], ['name' => 'idx_login_time'])
            ->setPrimaryKey('id')
            ->create();
    }

    public function down()
    {
        // 删除管理员登录日志表
        $this->dropTable('admin_login_log');
    }
} 